﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using 商品后台mvcdemo.Models;

namespace 商品后台mvcdemo.Controllers
{
    public class ProductInfoesController : Controller
    {
        private ProductDBEntities db = new ProductDBEntities();
        [HttpPost]
        public ActionResult Index(ProductInfo product)
        {
            string name = product.Name;
            List<ProductInfo> list = db.Set<ProductInfo>().ToList();
            if (name==null)
            {
                return View(list);
            }
            else
            {
                var sql =
                    from prod in list
                    where prod.Name.Contains(name)
                    select prod;
                return View(sql);
            }
        }
        // GET: ProductInfoes
        public ActionResult Index()
        {
            var productInfo = db.ProductInfo.Include(p => p.TypeInfo);
            return View(productInfo.ToList());
        }

        // GET: ProductInfoes/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProductInfo productInfo = db.ProductInfo.Find(id);
            if (productInfo == null)
            {
                return HttpNotFound();
            }
            return View(productInfo);
        }

        // GET: ProductInfoes/Create
        public ActionResult Create()
        {
            ViewBag.TypeId = new SelectList(db.TypeInfo, "TypeId", "TypeName");
            return View();
        }

        // POST: ProductInfoes/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性；有关
        // 更多详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Name,Price,AddTime,State,TypeId,Proimage")] ProductInfo productInfo)
        {
            if (ModelState.IsValid)
            {
                db.ProductInfo.Add(productInfo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.TypeId = new SelectList(db.TypeInfo, "TypeId", "TypeName", productInfo.TypeId);
            return View(productInfo);
        }

        // GET: ProductInfoes/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProductInfo productInfo = db.ProductInfo.Find(id);
            if (productInfo == null)
            {
                return HttpNotFound();
            }
            ViewBag.TypeId = new SelectList(db.TypeInfo, "TypeId", "TypeName", productInfo.TypeId);
            return View(productInfo);
        }

        // POST: ProductInfoes/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性；有关
        // 更多详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Name,Price,AddTime,State,TypeId,Proimage")] ProductInfo productInfo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(productInfo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TypeId = new SelectList(db.TypeInfo, "TypeId", "TypeName", productInfo.TypeId);
            return View(productInfo);
        }

        // GET: ProductInfoes/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ProductInfo productInfo = db.ProductInfo.Find(id);
            if (productInfo == null)
            {
                return HttpNotFound();
            }
            return View(productInfo);
        }

        // POST: ProductInfoes/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ProductInfo productInfo = db.ProductInfo.Find(id);
            db.ProductInfo.Remove(productInfo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
        [HttpGet]
        public ActionResult YznameKey(ProductInfo productInfo)
        {
            string name = productInfo.Name;
            List<ProductInfo> list = db.Set<ProductInfo>().ToList();
            var sql =
                from prodo in list
                where prodo.Name == name
                select prodo;
            if (sql.Count()>0)
            {
                return Content("true");
            }
            else
            {
                return Content("false");
            } 
        }
    }
}
